using Easislides.Properties;
using System;
using System.ComponentModel;
using System.Data.OleDb;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace Easislides
{
	public class FrmUsages : Form
	{
		private IContainer components = null;
		private ComboBox SessionList;
		private Button BtnCancel;
		private Button BtnReCalc;
		private Button BtnGenerate;
		private MonthCalendar CalendarFrom;
		private MonthCalendar CalendarTo;
		private Label label1;
		private Label label2;
		private RadioButton BtnOccurrences;
		private RadioButton BtnUsages;
		private ListView UsageDetails;
		private ColumnHeader columnHeader1;
		private ColumnHeader columnHeader2;
		private ColumnHeader columnHeader3;
		private ColumnHeader columnHeader4;
		private ColumnHeader columnHeader5;
		private ColumnHeader columnHeader6;
		private ColumnHeader columnHeader7;
		private ColumnHeader columnHeader8;
		private ListView SummaryDetails;
		private ColumnHeader columnHeader9;
		private ColumnHeader columnHeader10;
		private ColumnHeader columnHeader11;
		private ColumnHeader columnHeader12;
		private ContextMenuStrip CMenuUsageDetails;
		private ToolStripMenuItem CMenuUsageDetails_SelectAll;
		private ToolStripMenuItem CMenuUsageDetails_UnselectAll;
		private ToolStripMenuItem CMenuUsageDetails_Clear;
		private ToolStripMenuItem CMenuUsageDetails_Report;
		private ToolStripSeparator toolStripSeparator1;
		private ToolStripSeparator toolStripSeparator2;
		private ToolTip toolTip1;
		private Button BtnDelete;
		private string SelectedSession = "";
		private string CompleteQuery;
		private int sortColumnUsages = -1;
		private int sortColumnSummary = -1;
		private bool SongNumberUsed = false;
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            this.SessionList = new System.Windows.Forms.ComboBox();
            this.BtnCancel = new System.Windows.Forms.Button();
            this.BtnReCalc = new System.Windows.Forms.Button();
            this.CalendarFrom = new System.Windows.Forms.MonthCalendar();
            this.CalendarTo = new System.Windows.Forms.MonthCalendar();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.BtnOccurrences = new System.Windows.Forms.RadioButton();
            this.BtnUsages = new System.Windows.Forms.RadioButton();
            this.UsageDetails = new System.Windows.Forms.ListView();
            this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader3 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader4 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader5 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader6 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader7 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader8 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.CMenuUsageDetails = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.CMenuUsageDetails_SelectAll = new System.Windows.Forms.ToolStripMenuItem();
            this.CMenuUsageDetails_UnselectAll = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            this.CMenuUsageDetails_Clear = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
            this.CMenuUsageDetails_Report = new System.Windows.Forms.ToolStripMenuItem();
            this.SummaryDetails = new System.Windows.Forms.ListView();
            this.columnHeader9 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader10 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader11 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader12 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
            this.BtnDelete = new System.Windows.Forms.Button();
            this.BtnGenerate = new System.Windows.Forms.Button();
            this.CMenuUsageDetails.SuspendLayout();
            this.SuspendLayout();
            // 
            // SessionList
            // 
            this.SessionList.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.SessionList.FormattingEnabled = true;
            this.SessionList.Location = new System.Drawing.Point(12, 9);
            this.SessionList.Name = "SessionList";
            this.SessionList.Size = new System.Drawing.Size(220, 21);
            this.SessionList.TabIndex = 0;
            // 
            // BtnCancel
            // 
            this.BtnCancel.Location = new System.Drawing.Point(159, 35);
            this.BtnCancel.Name = "BtnCancel";
            this.BtnCancel.Size = new System.Drawing.Size(73, 24);
            this.BtnCancel.TabIndex = 5;
            this.BtnCancel.Text = "Close";
            this.BtnCancel.Click += new System.EventHandler(this.BtnCancel_Click);
            // 
            // BtnReCalc
            // 
            this.BtnReCalc.Location = new System.Drawing.Point(12, 35);
            this.BtnReCalc.Name = "BtnReCalc";
            this.BtnReCalc.Size = new System.Drawing.Size(80, 24);
            this.BtnReCalc.TabIndex = 4;
            this.BtnReCalc.Text = "Refresh";
            this.BtnReCalc.Click += new System.EventHandler(this.BtnReCalc_Click);
            // 
            // CalendarFrom
            // 
            this.CalendarFrom.Location = new System.Drawing.Point(12, 79);
            this.CalendarFrom.Name = "CalendarFrom";
            this.CalendarFrom.TabIndex = 6;
            // 
            // CalendarTo
            // 
            this.CalendarTo.Location = new System.Drawing.Point(12, 257);
            this.CalendarTo.Name = "CalendarTo";
            this.CalendarTo.TabIndex = 7;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(14, 63);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(78, 13);
            this.label1.TabIndex = 13;
            this.label1.Text = "Period From:";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2.Location = new System.Drawing.Point(14, 241);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(66, 13);
            this.label2.TabIndex = 14;
            this.label2.Text = "Period To:";
            // 
            // BtnOccurrences
            // 
            this.BtnOccurrences.Appearance = System.Windows.Forms.Appearance.Button;
            this.BtnOccurrences.Location = new System.Drawing.Point(333, 9);
            this.BtnOccurrences.Name = "BtnOccurrences";
            this.BtnOccurrences.Size = new System.Drawing.Size(80, 22);
            this.BtnOccurrences.TabIndex = 2;
            this.BtnOccurrences.Text = "Occurrences";
            this.BtnOccurrences.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // BtnUsages
            // 
            this.BtnUsages.Appearance = System.Windows.Forms.Appearance.Button;
            this.BtnUsages.Checked = true;
            this.BtnUsages.Location = new System.Drawing.Point(250, 9);
            this.BtnUsages.Name = "BtnUsages";
            this.BtnUsages.Size = new System.Drawing.Size(80, 22);
            this.BtnUsages.TabIndex = 1;
            this.BtnUsages.TabStop = true;
            this.BtnUsages.Text = "Full Details";
            this.BtnUsages.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.BtnUsages.CheckedChanged += new System.EventHandler(this.BtnUsages_CheckedChanged);
            // 
            // UsageDetails
            // 
            this.UsageDetails.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader1,
            this.columnHeader2,
            this.columnHeader3,
            this.columnHeader4,
            this.columnHeader5,
            this.columnHeader6,
            this.columnHeader7,
            this.columnHeader8});
            this.UsageDetails.ContextMenuStrip = this.CMenuUsageDetails;
            this.UsageDetails.FullRowSelect = true;
            this.UsageDetails.Location = new System.Drawing.Point(251, 36);
            this.UsageDetails.Name = "UsageDetails";
            this.UsageDetails.Size = new System.Drawing.Size(491, 377);
            this.UsageDetails.TabIndex = 8;
            this.UsageDetails.UseCompatibleStateImageBehavior = false;
            this.UsageDetails.View = System.Windows.Forms.View.Details;
            this.UsageDetails.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.UsageDetails_ColumnClick);
            // 
            // columnHeader1
            // 
            this.columnHeader1.Text = "Date";
            this.columnHeader1.Width = 68;
            // 
            // columnHeader2
            // 
            this.columnHeader2.Text = "Session";
            this.columnHeader2.Width = 118;
            // 
            // columnHeader3
            // 
            this.columnHeader3.Text = "Song Title";
            this.columnHeader3.Width = 143;
            // 
            // columnHeader4
            // 
            this.columnHeader4.Text = "No.";
            this.columnHeader4.Width = 54;
            // 
            // columnHeader5
            // 
            this.columnHeader5.Text = "Admin1";
            // 
            // columnHeader6
            // 
            this.columnHeader6.Text = "Admin2";
            this.columnHeader6.Width = 58;
            // 
            // columnHeader7
            // 
            this.columnHeader7.Text = "Song ID";
            this.columnHeader7.Width = 56;
            // 
            // columnHeader8
            // 
            this.columnHeader8.Text = "Sys ID";
            this.columnHeader8.Width = 53;
            // 
            // CMenuUsageDetails
            // 
            this.CMenuUsageDetails.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.CMenuUsageDetails_SelectAll,
            this.CMenuUsageDetails_UnselectAll,
            this.toolStripSeparator1,
            this.CMenuUsageDetails_Clear,
            this.toolStripSeparator2,
            this.CMenuUsageDetails_Report});
            this.CMenuUsageDetails.Name = "ContextMenuBibleText";
            this.CMenuUsageDetails.Size = new System.Drawing.Size(235, 104);
            // 
            // CMenuUsageDetails_SelectAll
            // 
            this.CMenuUsageDetails_SelectAll.Name = "CMenuUsageDetails_SelectAll";
            this.CMenuUsageDetails_SelectAll.Size = new System.Drawing.Size(234, 22);
            this.CMenuUsageDetails_SelectAll.Text = "Select &All";
            this.CMenuUsageDetails_SelectAll.Click += new System.EventHandler(this.CMenuUsageDetails_SelectAll_Click);
            // 
            // CMenuUsageDetails_UnselectAll
            // 
            this.CMenuUsageDetails_UnselectAll.Name = "CMenuUsageDetails_UnselectAll";
            this.CMenuUsageDetails_UnselectAll.Size = new System.Drawing.Size(234, 22);
            this.CMenuUsageDetails_UnselectAll.Text = "&Unselect All";
            this.CMenuUsageDetails_UnselectAll.Click += new System.EventHandler(this.CMenuUsageDetails_UnselectAll_Click);
            // 
            // toolStripSeparator1
            // 
            this.toolStripSeparator1.Name = "toolStripSeparator1";
            this.toolStripSeparator1.Size = new System.Drawing.Size(231, 6);
            // 
            // CMenuUsageDetails_Clear
            // 
            this.CMenuUsageDetails_Clear.Name = "CMenuUsageDetails_Clear";
            this.CMenuUsageDetails_Clear.Size = new System.Drawing.Size(234, 22);
            this.CMenuUsageDetails_Clear.Text = "Delete Selected Usage Records";
            this.CMenuUsageDetails_Clear.Click += new System.EventHandler(this.CMenuUsageDetails_Clear_Click);
            // 
            // toolStripSeparator2
            // 
            this.toolStripSeparator2.Name = "toolStripSeparator2";
            this.toolStripSeparator2.Size = new System.Drawing.Size(231, 6);
            // 
            // CMenuUsageDetails_Report
            // 
            this.CMenuUsageDetails_Report.Name = "CMenuUsageDetails_Report";
            this.CMenuUsageDetails_Report.Size = new System.Drawing.Size(234, 22);
            this.CMenuUsageDetails_Report.Text = "Generate Usage Report";
            this.CMenuUsageDetails_Report.Click += new System.EventHandler(this.CMenuUsageDetails_Report_Click);
            // 
            // SummaryDetails
            // 
            this.SummaryDetails.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader9,
            this.columnHeader10,
            this.columnHeader11,
            this.columnHeader12});
            this.SummaryDetails.FullRowSelect = true;
            this.SummaryDetails.Location = new System.Drawing.Point(251, 35);
            this.SummaryDetails.MultiSelect = false;
            this.SummaryDetails.Name = "SummaryDetails";
            this.SummaryDetails.Size = new System.Drawing.Size(436, 377);
            this.SummaryDetails.TabIndex = 9;
            this.SummaryDetails.UseCompatibleStateImageBehavior = false;
            this.SummaryDetails.View = System.Windows.Forms.View.Details;
            this.SummaryDetails.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.SummaryDetails_ColumnClick);
            // 
            // columnHeader9
            // 
            this.columnHeader9.Text = "Occurrence";
            this.columnHeader9.Width = 71;
            // 
            // columnHeader10
            // 
            this.columnHeader10.Text = "Song Title";
            this.columnHeader10.Width = 248;
            // 
            // columnHeader11
            // 
            this.columnHeader11.Text = "No.";
            this.columnHeader11.Width = 64;
            // 
            // columnHeader12
            // 
            this.columnHeader12.Text = "ID";
            // 
            // BtnDelete
            // 
            this.BtnDelete.Image = global::Properties.Resources.deleteW;
            this.BtnDelete.Location = new System.Drawing.Point(446, 9);
            this.BtnDelete.Name = "BtnDelete";
            this.BtnDelete.Size = new System.Drawing.Size(24, 24);
            this.BtnDelete.TabIndex = 15;
            this.toolTip1.SetToolTip(this.BtnDelete, "Delete Selected Usage Records");
            this.BtnDelete.Click += new System.EventHandler(this.BtnDelete_Click);
            // 
            // BtnGenerate
            // 
            this.BtnGenerate.Image = global::Properties.Resources.word1;
            this.BtnGenerate.Location = new System.Drawing.Point(419, 9);
            this.BtnGenerate.Name = "BtnGenerate";
            this.BtnGenerate.Size = new System.Drawing.Size(24, 24);
            this.BtnGenerate.TabIndex = 3;
            this.toolTip1.SetToolTip(this.BtnGenerate, "Generate Usages Report");
            this.BtnGenerate.Click += new System.EventHandler(this.BtnGenerate_Click);
            // 
            // FrmUsages
            // 
            this.ClientSize = new System.Drawing.Size(731, 425);
            this.Controls.Add(this.BtnDelete);
            this.Controls.Add(this.BtnOccurrences);
            this.Controls.Add(this.BtnUsages);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.CalendarTo);
            this.Controls.Add(this.CalendarFrom);
            this.Controls.Add(this.BtnGenerate);
            this.Controls.Add(this.BtnReCalc);
            this.Controls.Add(this.BtnCancel);
            this.Controls.Add(this.SessionList);
            this.Controls.Add(this.UsageDetails);
            this.Controls.Add(this.SummaryDetails);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "FrmUsages";
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Usages";
            this.Load += new System.EventHandler(this.FrmViewUsages_Load);
            this.CMenuUsageDetails.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		public FrmUsages()
		{
			this.InitializeComponent();
		}
		private void FrmViewUsages_Load(object sender, EventArgs e)
		{
			this.CalendarFrom.SetDate(DateTime.Now.Subtract(TimeSpan.FromDays(91.0)));
			this.SessionList.Text = "";
			this.ReCalc();
		}
		private void LoadSessions()
		{
			if (this.UsageDetails.Items.Count > 0)
			{
				for (int i = 0; i <= this.UsageDetails.Items.Count - 1; i++)
				{
					this.AddItemToSessionList(this.UsageDetails.Items[i].SubItems[1].Text);
				}
			}
			this.SessionList.Text = this.SelectedSession;
		}
		private void AddItemToSessionList(string InSession)
		{
			if (this.SessionList.Items.Count > 0)
			{
				for (int i = 0; i <= this.SessionList.Items.Count - 1; i++)
				{
					if (InSession == this.SessionList.Items[i].ToString())
					{
						return;
					}
				}
			}
			else
			{
				this.SessionList.Items.Add("");
			}
			this.SessionList.Items.Add(InSession);
		}
		private bool LoadExtracts()
		{
			bool result;
			if (!gf.ValidateDB(gf.DatabaseType.Usages))
			{
				result = false;
			}
			else
			{
				this.SelectedSession = this.SessionList.Text;
				this.SessionList.Items.Clear();
				this.UsageDetails.Items.Clear();
				this.UsageDetails.Columns[3].Width = 0;
				SortOrder sorting = this.UsageDetails.Sorting;
				this.UsageDetails.Sorting = SortOrder.None;
				ListViewItem listViewItem = new ListViewItem();
				bool flag = true;
				this.SongNumberUsed = false;
				try
				{
					OleDbConnection oleDbConnection = new OleDbConnection(gf.ConnectStringUsageDB);
					oleDbConnection.Open();
					this.CompleteQuery = "SELECT * FROM [USAGE] WHERE WORSHIP_DATE >= @WORSHIP_DATE_FROM and WORSHIP_DATE <= @WORSHIP_DATE_TO order by WORSHIP_DATE";
					OleDbCommand oleDbCommand = new OleDbCommand(this.CompleteQuery, oleDbConnection);
					oleDbCommand.Parameters.AddWithValue("@WORSHIP_DATE_FROM", this.CalendarFrom.SelectionStart.Date);
					oleDbCommand.Parameters.AddWithValue("@WORSHIP_DATE_TO", this.CalendarTo.SelectionStart.Date);
					oleDbCommand.CommandText = this.CompleteQuery;
					OleDbDataReader oleDbDataReader = oleDbCommand.ExecuteReader();
					while (oleDbDataReader.Read())
					{
						try
						{
							string text = gf.ObjToString(oleDbDataReader["WORSHIP_LIST"]);
							this.AddItemToSessionList(text);
							if (this.SelectedSession == "" || text == this.SelectedSession)
							{
								DateTime dateTime = (DateTime)oleDbDataReader["WORSHIP_DATE"];
								listViewItem = this.UsageDetails.Items.Add(dateTime.ToString("yyyy-MM-dd"));
								listViewItem.SubItems.Add(text);
								listViewItem.SubItems.Add(gf.RemoveMusicSym(gf.ObjToString(oleDbDataReader["SONG_TITLE"])));
								string text2 = gf.ObjToString(oleDbDataReader["SONG_NUMBER"]);
								if (text2 != "" && text2 != "0")
								{
									this.SongNumberUsed = true;
								}
								listViewItem.SubItems.Add(text2);
								listViewItem.SubItems.Add(gf.ObjToString(oleDbDataReader["ADMIN_1"]));
								listViewItem.SubItems.Add(gf.ObjToString(oleDbDataReader["ADMIN_2"]));
								listViewItem.SubItems.Add(gf.ObjToString(oleDbDataReader["SONG_ID"]));
								listViewItem.SubItems.Add(gf.ObjToString(oleDbDataReader["REC_ID"]));
							}
						}
						catch (Exception var_9_2AA)
						{
							flag = false;
						}
					}
					oleDbConnection.Close();
				}
				catch
				{
				}
				if (this.SongNumberUsed)
				{
					this.UsageDetails.Columns[3].Width = 54;
				}
				this.UsageDetails.Sorting = sorting;
				lv.Sort(ref this.UsageDetails, ref this.sortColumnUsages, this.sortColumnUsages, false);
				this.SessionList.Text = this.SelectedSession;
				result = flag;
			}
			return result;
		}
		private void LoadSummary()
		{
			this.SummaryDetails.Columns[2].Width = 0;
			ListViewItem listViewItem = new ListViewItem();
			this.SummaryDetails.Items.Clear();
			SortOrder sorting = this.SummaryDetails.Sorting;
			this.SummaryDetails.Sorting = SortOrder.None;
			if (this.UsageDetails.Items.Count > 0)
			{
				for (int i = 0; i <= this.UsageDetails.Items.Count - 1; i++)
				{
					this.AddItemToRank(i);
				}
			}
			if (this.SongNumberUsed)
			{
				this.SummaryDetails.Columns[2].Width = 64;
			}
			this.SummaryDetails.Sorting = sorting;
			lv.Sort(ref this.SummaryDetails, ref this.sortColumnSummary, this.sortColumnSummary, false);
		}
		private void AddItemToRank(int InIndex)
		{
			try
			{
				string text = this.UsageDetails.Items[InIndex].SubItems[6].Text;
				if (this.SummaryDetails.Items.Count > 0)
				{
					for (int i = 0; i <= this.SummaryDetails.Items.Count - 1; i++)
					{
						if (text == this.SummaryDetails.Items[i].SubItems[3].Text)
						{
							this.SummaryDetails.Items[i].SubItems[0].Text = (gf.StringToInt(this.SummaryDetails.Items[i].SubItems[0].Text) + 1).ToString();
							return;
						}
					}
				}
			}
			catch
			{
			}
			ListViewItem listViewItem = new ListViewItem();
			listViewItem = this.SummaryDetails.Items.Add("1");
			listViewItem.SubItems.Add(this.UsageDetails.Items[InIndex].SubItems[2].Text);
			listViewItem.SubItems.Add(this.UsageDetails.Items[InIndex].SubItems[3].Text);
			listViewItem.SubItems.Add(this.UsageDetails.Items[InIndex].SubItems[6].Text);
		}
		private void SortSummaryDetails()
		{
			lv.Sort(ref this.SummaryDetails, ref this.sortColumnSummary, 0, true);
		}
		private void BtnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}
		private void BtnReCalc_Click(object sender, EventArgs e)
		{
			this.ReCalc();
		}
		private void ReCalc()
		{
			this.LoadExtracts();
			this.LoadSummary();
		}
		private void CMenuUsageDetails_SelectAll_Click(object sender, EventArgs e)
		{
			this.UsageDetails_SelectAll();
		}
		private void CMenuUsageDetails_UnselectAll_Click(object sender, EventArgs e)
		{
			this.UsageDetails_UnselectAll();
		}
		private void CMenuUsageDetails_Clear_Click(object sender, EventArgs e)
		{
			this.DeleteSelectedUsageDetails();
		}
		private void UsageDetails_SelectAll()
		{
			if (this.UsageDetails.Items.Count > 0)
			{
				for (int i = 0; i < this.UsageDetails.Items.Count; i++)
				{
					this.UsageDetails.Items[i].Selected = true;
				}
			}
		}
		private void UsageDetails_UnselectAll()
		{
			if (this.UsageDetails.Items.Count > 0)
			{
				for (int i = 0; i < this.UsageDetails.Items.Count; i++)
				{
					this.UsageDetails.Items[i].Selected = false;
				}
			}
		}
		private void DeleteSelectedUsageDetails()
		{
			if (!(this.UsageDetails.Items.Count == 0 | this.UsageDetails.SelectedItems.Count == 0))
			{
				try
				{
					if (MessageBox.Show("This will permanently delete the selected usages records. Please Yes to confirm the delete.", "Deleted Selected Usages", MessageBoxButtons.YesNo) == DialogResult.Yes)
					{
						for (int i = this.UsageDetails.SelectedItems.Count - 1; i >= 0; i--)
						{
							OleDbConnection oleDbConnection = new OleDbConnection(gf.ConnectStringUsageDB);
							OleDbCommand oleDbCommand = new OleDbCommand("Delete * FROM [USAGE] WHERE REC_ID = " + this.UsageDetails.SelectedItems[i].SubItems[7].Text, oleDbConnection);
							oleDbConnection.Open();
							oleDbCommand.ExecuteNonQuery();
							this.UsageDetails.SelectedItems[i].Remove();
							oleDbConnection.Close();
						}
					}
				}
				catch
				{
				}
				this.LoadSummary();
			}
		}
		private void BtnUsages_CheckedChanged(object sender, EventArgs e)
		{
			this.UsageDetails.Visible = this.BtnUsages.Checked;
			this.SummaryDetails.Visible = !this.UsageDetails.Visible;
		}
		private void UsageDetails_ColumnClick(object sender, ColumnClickEventArgs e)
		{
			lv.Sort(ref this.UsageDetails, ref this.sortColumnUsages, e.Column, true);
		}
		private void SummaryDetails_ColumnClick(object sender, ColumnClickEventArgs e)
		{
			lv.Sort(ref this.SummaryDetails, ref this.sortColumnSummary, e.Column, true);
		}
		private void BtnGenerate_Click(object sender, EventArgs e)
		{
			this.GenerateReport();
		}
		private void BtnDelete_Click(object sender, EventArgs e)
		{
			this.DeleteSelectedUsageDetails();
		}
		private void GenerateReport()
		{
			string text = gf.RootEasiSlidesDir + "Documents\\Song Usages.rtf";
			if (MessageBox.Show("This will overwrite previous report " + text + ". Press OK to proceed or Cancel to quit.", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
			{
				try
				{
					this.Cursor = Cursors.WaitCursor;
					StreamWriter streamWriter = new StreamWriter(text, false, Encoding.Default);
					try
					{
						gf.RTFNewLine = "\\b0\\i0\\ulnone\\par ";
						gf.RTFIndent[0] = "\\pard\\tx1200\\tx3500\\tx8200\\tx9000 ";
						string value = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Microsoft Sans Serif;}}\\viewkind1\\uc1\\pard\\f0\\fs20\\margr600\\margl1000\\margt900\\margb1000 ";
						streamWriter.Write(value);
						streamWriter.Write("\\b\\ul Usage Details: " + gf.RTFNewLine + gf.RTFNewLine);
						streamWriter.Write(string.Concat(new string[]
						{
							"\\b Period:\\b0  ",
							this.CalendarFrom.SelectionStart.ToString("yyyy-MM-dd"),
							" to ",
							this.CalendarTo.SelectionStart.ToString("yyyy-MM-dd"),
							" (yyyy-mm-dd)",
							gf.RTFNewLine
						}));
						streamWriter.Write(((this.SelectedSession == "") ? "All Worship Lists displayed" : ("Worship List Restricted to '" + this.SelectedSession + "'")) + gf.RTFNewLine + gf.RTFNewLine);
						streamWriter.Write(string.Concat(new string[]
						{
							gf.RTFIndent[0],
							"\\b Date\tWorship List\tSong Title",
							this.SongNumberUsed ? "\tNo." : "",
							"\tLic Admin",
							gf.RTFNewLine
						}));
						for (int i = 0; i <= this.UsageDetails.Items.Count - 1; i++)
						{
							string text2 = this.UsageDetails.Items[i].SubItems[4].Text;
							if (text2 == "")
							{
								text2 = this.UsageDetails.Items[i].SubItems[5].Text;
							}
							else
							{
								if (this.UsageDetails.Items[i].SubItems[5].Text != "")
								{
									text2 = text2 + "/" + this.UsageDetails.Items[i].SubItems[5].Text;
								}
							}
							streamWriter.Write(gf.UnicodeToAscii_RTF(string.Concat(new string[]
							{
								gf.RTFIndent[0],
								this.UsageDetails.Items[i].SubItems[0].Text,
								"\t",
								this.UsageDetails.Items[i].SubItems[1].Text,
								"\t",
								this.UsageDetails.Items[i].SubItems[2].Text,
								this.SongNumberUsed ? ("\t" + this.UsageDetails.Items[i].SubItems[3].Text) : "",
								"\t",
								text2,
								gf.RTFNewLine
							})));
						}
						gf.RTFIndent[0] = "\\pard\\tx1200\\tx6200 ";
						streamWriter.Write(gf.RTFNewLine + gf.RTFNewLine + gf.RTFNewLine);
						streamWriter.Write("\\b\\ul Occurrences:" + gf.RTFNewLine + gf.RTFNewLine);
						streamWriter.Write(string.Concat(new string[]
						{
							"\\b Period:\\b0  ",
							this.CalendarFrom.SelectionStart.ToString("yyyy-MM-dd"),
							" to ",
							this.CalendarTo.SelectionStart.ToString("yyyy-MM-dd"),
							" (yyyy-mm-dd)",
							gf.RTFNewLine
						}));
						streamWriter.Write(((this.SelectedSession == "") ? "All Worship Lists displayed" : ("Worship List Restricted to '" + this.SelectedSession + "'")) + gf.RTFNewLine + gf.RTFNewLine);
						streamWriter.Write(gf.RTFIndent[0] + "\\b Occurrence\tSong Title" + (this.SongNumberUsed ? "\tNo." : "") + gf.RTFNewLine);
						for (int i = 0; i <= this.SummaryDetails.Items.Count - 1; i++)
						{
							streamWriter.Write(gf.UnicodeToAscii_RTF(string.Concat(new string[]
							{
								gf.RTFIndent[0],
								this.SummaryDetails.Items[i].SubItems[0].Text,
								"\t",
								this.SummaryDetails.Items[i].SubItems[1].Text,
								"\t",
								this.SongNumberUsed ? ("\t" + this.SummaryDetails.Items[i].SubItems[2].Text) : "",
								gf.RTFNewLine
							})));
						}
						streamWriter.Write("}");
						streamWriter.Flush();
						streamWriter.Close();
						gf.RunProcess(text);
					}
					catch
					{
						streamWriter.Flush();
						streamWriter.Close();
						MessageBox.Show("Error generating report " + text + ". The document might still be open. Please close it first and try again.");
					}
				}
				catch
				{
					MessageBox.Show("Error generating report " + text + ". The document might still be open. Please close it first and try again.");
				}
				this.Cursor = Cursors.Default;
			}
		}
		private void CMenuUsageDetails_Report_Click(object sender, EventArgs e)
		{
			this.GenerateReport();
		}
	}
}
